package com.lazy.mongodemo.query;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lazy.mongo.util.MongoUtil;
import com.lazy.mongodemo.common.AbstractMongoClient;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;

import ch.qos.logback.core.filter.Filter;

/**
 * 查询位置信息
 * @author luodan
 *
 */
public class QueryPosition extends AbstractMongoClient {
    protected static final Logger logger = LoggerFactory.getLogger(QueryPosition.class);
    
    public QueryPosition(String url, String dbName, String collectionName){
        super(url, dbName, collectionName);
    }

    /**
     * 测试排序, 升序和降序
     */
    public void testQueryHostnoSort() {
        BasicDBObject query =new BasicDBObject();
        query.append("hostno", new BasicDBObject("$eq", "3200100"));

        logger.info("query={}", query);
        //executeCount(query);
        Bson sort = new BasicDBObject("posTime",-1);
        //Bson sort = null;
        executeCount(query);
        executeQuery(query, 0, 10, sort);
    }
    
    /**
     * 测试排序, 升序和降序
     */
    public void testPostimeSort() {
        BasicDBObject query =new BasicDBObject();
        query.append("hostno", new BasicDBObject("$eq", "3003376"));
        query.append("posTime", new BasicDBObject("$gte", MongoUtil.getTime("2016-02-17 10:25:0"))
                                        .append("$lt", MongoUtil.getTime("2016-02-18 10:54:00"))
                );

        logger.info("query={}", query);
        //executeCount(query);
        Bson sort = new BasicDBObject("posTime",-1);
        //Bson sort = null;
        //executeCount(query);
        executeQuery(query, 0, 10, sort);
    }
    
    /**
     * 查询一个矩形区域
     */
    public void testQuerySquare() {
        BasicDBObject query =new BasicDBObject();
        // query.append("hostno", new BasicDBObject("$lt", "3003099"));
        query.append("posTime", new BasicDBObject("$gte", MongoUtil.getTime("2016-02-17 10:25:00"))
                                        .append("$lt", MongoUtil.getTime("2016-02-18 08:54:00"))
                );

        logger.info("query={}", query);
        //executeCount(query);
        executeQuery(query, 0, 10, null, null);
    }
    
    public void executeCount(Bson bsonQuery) {
        long start = System.currentTimeMillis();
        long count = coll.count(bsonQuery);

        long endTime = System.currentTimeMillis();
        logger.info("count = {} takes time {}", count, (System.currentTimeMillis() - start));
    }
    
    public void executeDistinct(String fieldName, Bson bsonQuery) {
        PrintBlock<String> printBlock = new PrintBlock<String>(0);
        long start = System.currentTimeMillis();
        coll.distinct(fieldName, bsonQuery, String.class).forEach(printBlock);;

        long endTime = System.currentTimeMillis();
        logger.info("count = {} takes time {}", printBlock.getCount(), (System.currentTimeMillis() - start));
    }
    
    public void executeQuery(Bson bsonQuery, int skip, int limit, Bson bsonSort) {
        executeQuery(bsonQuery, skip, limit, bsonSort, QueryUtil.getProjectBsonPos());
    }
    
    public void executeQuery(Bson bsonQuery, int skip, int limit, Bson bsonSort, Bson bsonProjection) {
        PrintBlock printBlock = new PrintBlock(0);
        long start = System.currentTimeMillis();
        FindIterable<Document> filter = coll.find(bsonQuery);
        if (bsonSort != null) {
            filter.sort(bsonSort);
        }
        if (bsonProjection != null) {
            filter.projection(bsonProjection);
        }
        filter.skip(skip).limit(limit);
        filter.forEach(printBlock);
        
        long endTime = System.currentTimeMillis();
        logger.info("count = {} takes time {}", printBlock.getCount(), (System.currentTimeMillis() - start));
    }
}
